//
//  XDAttachChooseViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-25.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDAttachChooseViewController.h"
#import "XDAttachManagerListCell.h"
#import "UIViewController+MJPopupViewController.h"

@interface XDAttachChooseViewController ()
{
    NSMutableArray *filesArray;
}
@end

@implementation XDAttachChooseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    filesArray = [[NSMutableArray alloc]init];
    [self refreshFile];
}

-(void)refreshFile
{
    [filesArray removeAllObjects];
    NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/",NSHomeDirectory()];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileList = [[NSArray alloc] init];
    
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    fileList = [fileManager contentsOfDirectoryAtPath:savePath error:&error];
    
    for (int i=0; i<fileList.count; i++) {
        NSString *DirPath = [NSString stringWithFormat:@"%@/Documents/attaches/%@",NSHomeDirectory(),[fileList objectAtIndex:i]];
        NSArray *singleDirFilesList = [fileManager contentsOfDirectoryAtPath:DirPath error:&error];
        for (int j=0; j<singleDirFilesList.count; j++) {
            NSString *fileName = [singleDirFilesList objectAtIndex:j];
            NSString *singleFilePath = [NSString stringWithFormat:@"%@/%@",DirPath,fileName];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:fileName forKey:@"filename"];
            [dic setObject:singleFilePath forKey:@"filepath"];
            [dic setObject:@"1" forKey:@"type"];
            [dic setObject:@"" forKey:@"fileguid"];
            [filesArray addObject:dic];
            [dic release];
        }
    }
    [self.tv reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tv release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTv:nil];
    [super viewDidUnload];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellItem = @"CellItem";
    XDAttachManagerListCell *cell = (XDAttachManagerListCell *)[tableView dequeueReusableCellWithIdentifier:CellItem];
    if (cell==nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"XDAttachManagerListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSMutableDictionary *m = [filesArray objectAtIndex:indexPath.row];
    NSString *filename = [m objectForKey:@"filename"];
    if ([filename hasSuffix:@".doc"]||[filename hasSuffix:@".docx"]) {
        cell.ivIcon.image = [UIImage imageNamed:@"img_attach_doc"];
    }
    else if([filename hasSuffix:@"xls"]||[filename hasSuffix:@".xlsx"])
    {
        cell.ivIcon.image = [UIImage imageNamed:@"img_attach_excel"];
    }
    else if([filename hasSuffix:@"pdf"])
    {
        cell.ivIcon.image = [UIImage imageNamed:@"img_pdf_48"];
    }
    else if([filename hasSuffix:@"txt"])
    {
        cell.ivIcon.image = [UIImage imageNamed:@"img_txt_48"];
    }
    else if([filename hasSuffix:@"jpg"])
    {
        cell.ivIcon.image = [UIImage imageNamed:@"img_jpg_48"];
    }
    cell.lbFilename.text = [m objectForKey:@"filename"];

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return filesArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (IBAction)closeAction:(id)sender {
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *m = [filesArray objectAtIndex:indexPath.row];
    [self.delegate epointAttachSelected:m];
}

@end
